package cn.hp.customer.service.impl;

import cn.hp.customer.mapper.CouponMapper;
import cn.hp.customer.pojo.TblCoupon;
import cn.hp.customer.pojo.TblCouponSendInfo;
import cn.hp.customer.pojo.TblUserCoupon;
import cn.hp.customer.pojo.vo.CouponSendInfoVO;
import cn.hp.customer.pojo.vo.CouponVO;
import cn.hp.customer.service.CouponService;
import cn.hp.customer.util.ResponseData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private CouponMapper couponMapper ;

    @Override
    public ResponseData findAllByPage(int page, int limit, String couponName) {
        // 开启分页
        PageHelper.startPage(page,limit );
        // 使用mapper查询
        List<CouponVO> coupons = couponMapper.findAllByConditions(couponName);
        // 1.把所有字段放在一个类中 2.Map 3.使用A类{ B类 }
        PageInfo pageInfo = new PageInfo(coupons);
        return ResponseData.success(pageInfo);
    }

    @Override
    public ResponseData addCoupon(TblCoupon tblCoupon) {
        // 新增的业务
//        1. 当effective_type 值为1时 需要设置 结束时间
        if (tblCoupon.getEffectiveType() == 1 ){
            // 结束时间的设置
            tblCoupon.setExpireTime( new Date(tblCoupon.getEnableTime().getTime() + tblCoupon.getEffectiveDay()*24*60*60*1000));
        }
//        2. create_user当前登录系统管理员
        int count = couponMapper.insert(tblCoupon);
        if (count > 0){
            return ResponseData.success();
        }
        return ResponseData.fail("新增失败，请重试!");
    }

    @Override
    public ResponseData findAllSendByPage(int page, int limit) {
        // 开启分页
        PageHelper.startPage(page,limit );
        // 使用mapper查询
        List<CouponSendInfoVO> couponSendInfos = couponMapper.findAllSend();
        // 1.把所有字段放在一个类中 2.Map 3.使用A类{ B类 }
        PageInfo pageInfo = new PageInfo(couponSendInfos);
        return ResponseData.success(pageInfo);
    }

    @Override
    public ResponseData add(MultipartFile file, TblCouponSendInfo tblCouponSendInfo, File filePath) {
        // 给文件一个新的名称 UUID
        UUID uuid = UUID.randomUUID();
        String randomName = uuid.toString().replace("-", "");
        // 获取后缀
        // 获取上传的文件的全名称
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 将file 上传到对应的地址中
        try {
            file.transferTo(new File(filePath , randomName+suffix));
            //
            tblCouponSendInfo.setStatus(0);
        } catch (IOException e) {
            e.printStackTrace();
            tblCouponSendInfo.setStatus(1);
        }
        // 设置 status send_status
        // 针对send_status 调用短信接口 根据接口返回的值 设置成功失败
        tblCouponSendInfo.setSendStatus(0);
        // 2021-10-28/762a48c58fb84b409e3e3c81ebdc17bc.jpg
        tblCouponSendInfo.setImgUrl(filePath.getName()+File.separator + randomName+suffix);
        int count = couponMapper.add(tblCouponSendInfo);
        if (count > 0 ){
            return ResponseData.success();
        }
        // 如果没有插入数据，说明失败
        tblCouponSendInfo.setStatus(1);
        return ResponseData.fail("添加失败，稍后重试");
    }

    @Override
    public List<TblCouponSendInfo> findAllSendListCouponSendName() {
        return couponMapper.findAllSendList();
    }

    @Override
    public void addUserCoupon(TblUserCoupon tblUserCoupon) {
        couponMapper.addUserCoupon(tblUserCoupon);
    }

    @Override
    public int findCountCoupon(Integer couponId, Integer id) {
        return couponMapper.findCountByCouponIdAndCustomerId(couponId , id);
    }
}
